﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.XmlConfig
{
    public static class ConfigHelper
    {
        /// <summary>
        /// 取得配置信息存储在页面中的控件的Id
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public static string GetClientId(ConfigSetting setting)
        {
            return "WebCfg$" + setting.ParentGroup.Key + "$" + setting.Key;
        }

        /// <summary>
        /// 取得配置文件设置的控件
        /// </summary>
        /// <param name="setting"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string GetSettingHtmlControl(ConfigSetting setting)
        {
            string Id = GetClientId(setting);

            switch (setting.Type)
            {
                case SettingType.Bool:
                    return string.Format(
                        @"<select id='{0}' name='{0}' class='{2}'  value='{1}'>
                            <option value='True'>是</option>
                            <option value='False'>否</option>
                          </select>
                          <script type='text/javascript'> document.getElementById('{0}').value='{1}'</script>",
                        Id,
                        setting.Value,
                        setting.Type);
                case SettingType.Date:
                    return string.Format(
                        "<input id='{0}' name='{0}' class='{2}' type='date' value='{1}' />",
                        Id,
                        setting.Value,
                        setting.Type);
                case SettingType.Email:
                    return string.Format(
                        "<input id='{0}' name='{0}' class='{2}' type='email' value='{1}' />",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Flash:
                    return string.Format(
                        @"<span>{0}<span>&nbsp;
                            <input  runat='server'  id='{1}' name='{1}' type='file' />",
                        System.IO.Path.GetFileName(setting.Value),
                        Id);
                case SettingType.Html:
                    return string.Format(
                        "<textarea id='{0}' name='{0}' class='{2}' rows=\"3\" cols=\"20\">{1}</textarea>",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Image:
                    return string.Format(
                        "<img alt='{0}' src='{1}' class='{2}'  style='height: 20px; width: 20px;'> &nbsp;<input  runat='server'  id='{2}' name='{2}' type='file' />",
                        setting.Text,
                        setting.Value,
                        Id,
                        setting.Type);

                case SettingType.MultText:
                    return string.Format(
                        "<textarea id='{0}' name='{0}' class='{2}' rows=\"3\" cols=\"20\">{1}</textarea>",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Number:
                    return string.Format(
                        "<input id='{0}' name='{0}' class='{2}' type='number' value='{1}' />",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Password:
                    return string.Format(
                        "<input id='{0}' name='{0}' class='{2}'  type='password' value='{1}' />",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Range:
                    return string.Format(
                        "<input id='{0}' name='{0}' class='{2}' type='text' value='{1}' />",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Select:
                    string opts = "";
                    foreach (string opt in setting.Attrs["Options"].InnerText.Split(','))
                    {
                        opts += "<option value='True'>" + opt + "</option>";
                    }
                    return string.Format(
                        "<select id='{0}' name='{0}' class='{2}'  value='{1}'>"
                        + opts
                        + "</select><script type='text/javascript'> document.getElementById('{0}').value='{1}'</script>",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Text:
                    return string.Format(
                        "<input id='{0}' name='{0}' class='{2}' type='text' value='{1}' />",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Url:
                    return string.Format(
                        "<input id='{0}' name='{0}' class='{2}' type='url' value='{1}' />",
                        Id,
                        setting.Value,
                        setting.Type);

                case SettingType.Video:
                    return string.Format(
                        @"<span>{0}<span>&nbsp;
                            <input  runat='server'  id='{1}' name='{1}' type='file' />",
                        System.IO.Path.GetFileName(setting.Value),
                        Id);

                default:
                    return string.Format(
                        "<span id='{0}' class='{2}'  name='{0}'>{1}</span>",
                        Id,
                        setting.Value,
                        setting.Type);
            }
        }
    }
}
